package com.lxit.video.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lxit.video.bean.VideoComment;
import com.lxit.video.dao.VideoCommentDao;
import com.lxit.video.dao.VideoCommentRepository;

/**
 * @author 贺长全
 * @time 2018 下午3:46:37
 * @desc 自定义 Repository 实现
 */
public class VideoCommentRepositoryImpl implements VideoCommentDao {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public List<VideoComment> pageBackstageVideoComment(Integer pageIndex, Integer pageSize, String userNumber,
			String videoName, String stratTime, String entTime) {
		StringBuffer sql = new StringBuffer("from VideoComment v where 1=1 ");
		if (userNumber != null) {
			sql.append("and v.userInfo.userNumber like '%" + userNumber + "%' ");
		}
		if (videoName != null) {
			sql.append("and v.videoInfo.videoName like '%"+videoName+"%' ");
		}
		if (stratTime != null) {
			sql.append("and v.commentTime >=:stratTime ");
		}
		if (entTime != null) {
			sql.append("and v.commentTime <=:entTime ");
		}
		Query query = entityManager.createQuery(sql.toString());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (stratTime != null) {
			try {
				query.setParameter("stratTime", sdf.parse(stratTime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (entTime != null) {
			try {
				query.setParameter("entTime", sdf.parse(entTime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.getResultList();
	}
	
	@Override
	public int pageBackstageVideoCommentCount(String userNumber, String videoName, String stratTime, String entTime) {
		StringBuffer sql = new StringBuffer("select count(1) from video_comment v " + 
				"inner JOIN user_info u on v.commentator = u.user_id " + 
				"inner JOIN video_info i on v.video_id = i.video_id where 1=1 ");
		if (userNumber != null) {
			sql.append("and u.user_number like '%" + userNumber + "%' ");
		}
		if (videoName != null) {
			sql.append("and i.video_name like '%"+videoName+"%' ");
		}
		if (stratTime != null) {
			sql.append("and v.comment_time >=:stratTime ");
		}
		if (entTime != null) {
			sql.append("and v.comment_time <=:entTime ");
		}
		Query query = entityManager.createNativeQuery(sql.toString());
		if (stratTime != null) {
			query.setParameter("stratTime", stratTime);
		}
		if (entTime != null) {
			query.setParameter("entTime", entTime);
		}
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@Override
	public List<VideoComment> pageVideoComment(Integer pageIndex, Integer pageSize, Integer videoId) {
		String sql = "from VideoComment v  where v.videoInfo.videoId=:videoId order by v.commentId desc";
		Query query = entityManager.createQuery(sql);
		query.setParameter("videoId", videoId);
		query.setFirstResult((pageIndex - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.getResultList();
	}

	@Override
	public int pageVideoCommentCount(Integer videoId) {
		String sql = "select count(1) from video_comment v where v.video_id=:videoId ";
		Query query = entityManager.createNativeQuery(sql);
		query.setParameter("videoId", videoId);
		return Integer.parseInt(query.getSingleResult().toString());
	}

}
